﻿#include <stddef.h>
#include "n2base.h"

#ifndef N2_LINK_FILE_TABLE_9A9345FC54627535
#define N2_LINK_FILE_TABLE_9A9345FC54627535  1

#include "Coff.h"
#include "UniverTab.h"


class SrcFileBase : public UniverTabItem
{
public:
    FILE *                  m_fp;
    bool                    m_haveOpen;                                 // 文件是否已经被打开
    bool                    m_littleEndian;                             // 该文件是否little endian
    bool                    m_isInLib;                                  // obj是否在lib里面
    n2u32                   m_membSize;                                 // obj成员大小 (m_isInLib==true时有效)
    n2u32                   m_membOffset;                               // obj成员偏移 (m_isInLib==true时有效)

    size_t read(unsigned char* buffer, size_t readSize);
    size_t readInt16(n2u16 * p);
    size_t readInt32(n2u32 * p);
    size_t readInt64(n2u64 * p);
    int seek(size_t pos);
    int seekCur(int offset);
    int close();
};

class SrcFile : public SrcFileBase
{
public:
    SrcFile();
    SrcFile(const char * filename);
    virtual ~SrcFile();
    const char * getFileName();
    FILE * openForReadOnly();

protected:
    void init(const char * name);
    virtual void moveFrom(UniverTabItem *p);
    virtual void copyFrom(const UniverTabItem *p);
    virtual UniverTabItem * getOffsetItem(size_t index);

public:
    N2PoolString            m_filename;                                 // obj文件名
    n2u32                   m_startSectionId;                           // 该OBJ文件的第1个section在总section表中的ID
    n2u32                   m_sectionCount;                             // 该OBJ文件的section数
    Coff32_Symb *           m_symbolTab;                                // 该OBJ中的符号表(COFF原始格式)
    n2u32                   m_symbolCnt;                                // 该OBJ中的符号数
    char *                  m_stringTab;                                // 该OBJ中的字符串表
};

class LibFile : public SrcFileBase
{
public:
    LibFile();
    virtual ~LibFile();

protected:
    virtual void moveFrom(UniverTabItem *p);
    virtual void copyFrom(const UniverTabItem *p);
    virtual UniverTabItem * getOffsetItem(size_t index);

private:

};

class SrcFileTable : public UniverTab
{
private:
    virtual UniverTabItem* allocGroupData(size_t count);
public:
    SrcFile* getFile(size_t index);
    SrcFileTable(size_t  groupCubage);
    SrcFileTable();
};

class LibFileTable : public UniverTab
{
private:
    virtual UniverTabItem* allocGroupData(size_t count);
public:
    LibFile* getFile(size_t index);
    LibFileTable(size_t  groupCubage);
    LibFileTable();
};


enum InputFileException{
    INPUT_FILE_EXCEPTION_CANNOT_OPEN = 1,
    INPUT_FILE_EXCEPTION_EOF,
    INPUT_FILE_EXCEPTION_FORMAT_ERROR,
};


extern SrcFileTable * input_objs;                   // 输入的OBJ文件列表
extern LibFileTable * input_libs;                   // 输入的LIB文件列表




#endif // N2_LINK_FILE_TABLE_9A9345FC54627535



